package vn.keho.module.dompeto.socket;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonElement;

import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import vn.keho.define.convert.JsonUtil;
import vn.keho.module.dompeto.MerchantActivity;
import vn.keho.module.dompeto.rest.DompetoDefine;
import vn.keho.module.dompeto.rest.DompetoRequest;

/**
 * Created by KeHo on 2/8/15.
 */
public class TransactionSocket {
    private static TransactionSocket instance;
    private static final String SERVER_ADDRESS = "http://" + DompetoDefine.HOST + ":3000/transaction";
    private SocketIO socket;
    private MerchantActivity context;

    public static synchronized TransactionSocket getInstance(MerchantActivity context){
        if (instance == null) {
            instance = new TransactionSocket(context);
        }
        instance.context = context;
        return instance;
    }

    public void pushTransaction(String merchant) {
        TransactionMessage transactionMessage = new TransactionMessage();
        transactionMessage.setUsername(DompetoRequest.username);
        transactionMessage.setMerchant(merchant);
        getSocket().emit("push", JsonUtil.buildJsonString(transactionMessage));
    }

    public SocketIO getSocket(){
        if(this.socket == null || !this.socket.isConnected()) {
            this.socket = getTransactionServerSocket();
        }
        return this.socket;
    }

    private TransactionSocket(MerchantActivity context){
        this.context = context;
        this.socket = getTransactionServerSocket();
    }

    public void onDestroy() {
        getSocket().disconnect();
        instance = null;
    }

    private SocketIO getTransactionServerSocket(){
        try {
            final SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() {
                @Override
                public void onDisconnect() {
                    Log.i("Socket", "Disconnect Transaction Socket");
                }

                @Override
                public void onConnect() {
                    Log.i("Socket", "Connect Transaction Socket");
                    TransactionMessage transactionMessage = new TransactionMessage();
                    transactionMessage.setUsername(DompetoRequest.username);
                    transactionMessage.setToken(DompetoRequest.token);
                    getSocket().emit("merchant", JsonUtil.buildJsonString(transactionMessage));
                }

                @Override
                public void onError(SocketIOException e) {
                    Log.i("Socket", "Error message 2 ");
                    e.printStackTrace();

                }

                @Override
                public void onMessage(String data, IOAcknowledge ack) {
                    Log.i("Socket", "Receive message 1 ");
                }

                @Override
                public void onMessage(JsonElement json, IOAcknowledge ack) {
                    Log.i("Socket", "Receive message 2 ");
                }

                @Override
                public void on(String event, IOAcknowledge ack, JsonElement... args) {
                    if(event.equals("inbox")) {
                        //Update Notify inbox
                        TransactionMessage transactionMessage = JsonUtil.generateJavaObject(args[0].toString(), TransactionMessage.class);
                        context.onInboxUpdateNotify(transactionMessage.getInboxCount());
                    }
                    if (event.equals("chatMessage")) {
                        //JSONObject json = (JSONObject) args[0];
                        /*ChatMessage chatMessage = new ChatMessage(json);

                        Intent intent = new Intent();
                        intent.setAction("newChatMessage");
                        intent.putExtra("chatMessage", chatMessage);
                        context.sendBroadcast(intent);*/
                    }
                }
            });
            return socket;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
